const Koa = require('koa');
const app = new Koa();
const Router = require('koa-router');
let router = new Router();

const mongoose = require('mongoose');
const fs = require('fs');

router.get('/insertAllGoodsInfo',async(ctx)=>{
    fs.readFile('./data_json/newGoods.json','utf8',(err,data)=>{
       // console.log('data:',data)
       let goodsData = JSON.parse(data);
       // console.log('goods',goodsData)
        let saveCount =0;
        const Goods = mongoose.model('Goods');
        goodsData.map((value)=>{
           // console.log('value',value);
            let newGoods = new Goods(value);
            newGoods.save().then(()=>{
                saveCount++;
                console.log('成功'+saveCount);
            }).catch((err)=>{
                console.log('失败'+err)
            })

        })
    })
    ctx.body ='开始导入数据'
})

router.get('/insertAllCategory',async(ctx)=>{
    fs.readFile('./data_json/category.json','utf8',(err,data)=>{        
        let categorData= JSON.parse(data);
        let saveCount =0;
        const Category = mongoose.model('Category');
       
        categorData.RECORDS.map((value)=>{
            let goodsCategorys = new Category(value);
            goodsCategorys.save().then(()=>{
                saveCount++;
                console.log('成功'+saveCount)
            }).catch(error=>{
                console.log("失败:"+error)
            })
        })
    })
    ctx.body ="开始导入商品类别"
})

router.get('/insertAllCategorySub',async(ctx)=>{
    fs.readFile('./data_json/category_sub.json','utf8',(err,data)=>{
       let categorySubData= JSON.parse(data);
        let saveCount =0;
        console.log('categorySubData');
        const Category = mongoose.model('CategorySub');
        categorySubData.RECORDS.map((value,index)=>{
            let goodsCategorySub = new Category(value);
            goodsCategorySub.save().then(()=>{
                saveCount++;
                console.log('成功'+saveCount)
            }).catch(error=>{
                console.log("失败:"+error)
            })
        })
    })
    ctx.body ="开始导入商品子类别"
})

router.post('/getDetailGoodsInfo',async(ctx)=>{
    try{
        let goodsId = ctx.request.body.goodsId;
        const Goods = mongoose.model('Goods');
        let result =await Goods.findOne({ID:goodsId}).exec();
        ctx.body = {code:200,message:result}
    }

    catch(error){
        ctx.body = {code:500, message:error}
    }
})

router.get('/getCategoryList',async(ctx)=>{
    try{
        const Ctategory = mongoose.model('Category');
        let result = await Ctategory.find().exec();
        ctx.body = {code:200,message:result}
    }
    catch(error){
        ctx.body = {code:500, message:error}
    }
})

router.post('/getCategorySubList',async(ctx)=>{
    try{        
        let categoryId = ctx.request.body.categoryId
        console.log(categoryId);
        const CtategorySub = mongoose.model('CategorySub');
        let result = await CtategorySub.find({MALL_CATEGORY_ID:categoryId}).exec();
        ctx.body = {code:200,message:result}
    }
    catch(error){
        ctx.body = {code:500, message:error}
    }
})
//根据类别获取商品列表
router.post('/getGoodsListByCategorySubID',async(ctx)=>{
    try{
     // let categorySubId = ctx.request.body.categoryId;//子类别Id
    let categorySubId = ctx.request.body.categorySubId;
        const num = 10;//每页显示数量
        let page =ctx.request.body.page;//当前页数
        let start = (page-1)*num; //开始位置
       
        const Goods = mongoose.model('Goods');
        let result = await Goods.find({SUB_ID:categorySubId}).skip(start).limit(num).exec();//从哪里开始读，每页读多少条
        ctx.body = {code:200,message:result}
    }
    catch(error){
        ctx.body = {code:500, message:error}
    }
})

module.exports = router;